<template>
  <div>
    <el-table border :data="records">
      <el-table-column
        label="序号"
        type="index"
        align="center"
        width="80"
      ></el-table-column>
      <el-table-column label="名称" prop="skuName"></el-table-column>
      <el-table-column label="描述" prop="skuDesc"></el-table-column>
      <el-table-column label="默认图片">
        <template slot-scope="{ row, $index }">
          <img :src="row.skuDefaultImg" style="width: 100px; height: 100px" />
        </template>
      </el-table-column>
      <el-table-column
        label="重量(KG)"
        prop="weight"
        width="80"
      ></el-table-column>
      <el-table-column
        label="价格(元)"
        prop="price"
        width="80"
      ></el-table-column>
      <el-table-column label="操作" width="width">
        <template slot-scope="{ row, $index }">
          <el-button
            type="success"
            size="mini"
            icon="el-icon-download"
            v-if="row.isSale == 0"
            @click="sale(row)"
            style="margin: 5px 0px"
            title="是否上架操作"
          ></el-button>
          <el-button
            type="success"
            size="mini"
            icon="el-icon-upload2"
            title="是否上架操作"
            @click="cancel(row)"
            v-else
          ></el-button>
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            title="修改sku"
            @click="edit"
          ></el-button>
          <el-button
            type="info"
            size="mini"
            icon="el-icon-info"
            title="查看sku详情"
            @click="getSkuInfo(row)"
          ></el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            title="删除sku"
            @click="records.splice(row, 1)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="text-align: center"
      :current-page="page"
      :page-sizes="[3, 5, 10]"
      :page-size="limit"
      layout="prev,pager,next,jumper,->,sizes,total"
      :total="total"
      @current-change="current_change"
      @size-change="size_change"
    >
    </el-pagination>
    <!-- 抽屉效果 -->
    <el-drawer :visible.sync="show" :show-close="false" size="50%">
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{ skuInfo.price }}元</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="16">
          <template>
            <el-tag
              type="success"
              v-for="(attr, index) in skuInfo.skuAttrValueList"
              :key="attr.id"
              style="margin-right: 10px"
              >{{ attr.attrId }}---{{ attr.valueId }}</el-tag
            >
          </template>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16">
          <el-carousel height="150px">
            <el-carousel-item
              v-for="(imgs, index) in skuInfo.skuImageList"
              :key="imgs.id"
            >
              <img :src="imgs.imgUrl" alt="" style="width: 100%" />
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: "Sku",
  data() {
    return {
      page: 1,
      limit: 10,
      total: 0,
      records: [], //存储sku列表的数据
      skuInfo: {}, //存储sku的详情信息
      show: false, //显示与隐藏抽屉效果
    };
  },
  mounted() {
    this.getSkuList();
  },
  methods: {
    //获取sku列表的方法
    async getSkuList() {
      const { page, limit } = this;
      let result = await this.$API.sku.reqSkuList(page, limit);
      // console.log(result);
      if (result.code == 200) {
        this.records = result.data.records;
        this.total = result.data.total;
      }
    },
    //分页器当前页发生变化的回调
    current_change(page) {
      this.page = page;
      this.getSkuList();
    },
    //分页器每页的条数发生变化的回调
    size_change(limit) {
      this.limit = limit;
      this.getSkuList();
    },
    //上架按钮
    async sale(row) {
      let result = await this.$API.sku.reqSale(row.id);
      if (result.code == 200) {
        this.$message({ type: "success", message: "上架成功" });
        row.isSale = 1;
      }
    },
    //下架按钮
    async cancel(row) {
      let result1 = await this.$API.sku.reqCancel(row.id);
      if (result1.code == 200) {
        this.$message({ type: "success", message: "下架成功" });
        row.isSale = 0;
      }
    },
    //开发中的按钮
    edit() {
      this.$message("正在开发中...敬请期待");
    },
    //sku详情按钮的回调
    async getSkuInfo(row) {
      //抽屉显示
      this.show = true;
      //发请求获取数据
      let result = await this.$API.sku.reqSkuById(row.id);
      if (result.code == 200) {
        this.skuInfo = result.data;
      }
    },
  },
};
</script>

<style scoped>
.el-row .el-col-5 {
  font-size: 18px;
  text-align: center;
}
.el-col {
  margin: 10px 10px;
}
</style>